using System;
using Adoor.Expression.AST;

namespace Adoor.Expression
{
	/// <summary>
	/// Summary description for ExpressionReplace.
	/// </summary>
	public class ExpressionReplace : ExpressionCloner
	{
		private string newQualifier;
		private string oldQualifier;
		public static ExpressionNode ReplaceNameQualifer(ExpressionNode n, string oldQualifier, string newQualifier)
		{
			ExpressionReplace es = new ExpressionReplace(oldQualifier, newQualifier);
			n.Accept(es);
			return es.result;
		}

		protected ExpressionReplace(string oldQualifier, string newQualifier)
		{
			this.newQualifier = newQualifier;
			this.oldQualifier = oldQualifier;
		}

		public override void VisitName(ExpressionName n)
		{
			if(n.Qualifier.ToLower() == oldQualifier.ToLower()
				&& newQualifier != null)
			{
				this.result = new ExpressionName(newQualifier, n.Value);
			}
			else
				base.VisitName(n);
		}

	}
}
